/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android app
 * Create Time: 16-2-16 下午6:43
 */

package com.lenovo.mso2o.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.lenovo.framework.util.maputil.MapDistance;
import com.lenovo.mso2o.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PoiSearchingDetailAdapter extends RecyclerView.Adapter<PoiSearchingDetailAdapter.ViewHolder>  {
    private List<PoiInfo> poiInfoList;
    private LatLng mLatLng;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public PoiSearchingDetailAdapter(List<PoiInfo> items) {
        setList(items);
    }

    public PoiInfo getItem(int position){
        return poiInfoList.get(position);
    }

    public void setList(List<PoiInfo> items){
        if(items == null)
            this.poiInfoList = new ArrayList<>();
        else
            this.poiInfoList = items;
        notifyDataSetChanged();
    }

    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.poi_detail_item, parent, false);
        return new ViewHolder(v);
    }

    @Override public void onBindViewHolder(ViewHolder holder, int position) {
        PoiInfo item = poiInfoList.get(position);
        holder.detailTitle.setText((position + 1) + ":" + item.name);
        holder.address.setText(item.address);
        if(mLatLng != null) {
            //double lon1, double lat1, double lon2, double lat2
            String dis = MapDistance.getInstance().getDistance(mLatLng, item.location);
            holder.distance.setText(dis);
        }else{
            holder.distance.setText("");
        }
    }

    @Override public int getItemCount() {
        return poiInfoList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.tv_poi_detail_title) TextView detailTitle;
        @Bind(R.id.tv_poi_detail_dis) TextView distance;
        @Bind(R.id.tv_detail_address) TextView address;

        public ViewHolder(final View rootView) {
            super(rootView);
            ButterKnife.bind(this, rootView);
            rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null && getLayoutPosition() != -1) {
                        mOnItemClickListener.onItemClick(getLayoutPosition());
                    }
                }
            });

        }

    }

    public void setLatLng(LatLng latLng){
        this.mLatLng = latLng;
    }
}